package com.superman.view;

import com.superman.entity.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class LoginUI {

    private JFrame frame = new JFrame("登录");
    private Container container = frame.getContentPane();
    private JTextField username = new JTextField();
    private JPasswordField password = new JPasswordField();
    private JButton login = new JButton("登录");
    private JButton register = new JButton("注册");

    public LoginUI(){
        frame.setSize(300,200);
        container.setLayout(new BorderLayout());//设置容器为边界布局管理器
        initFrame();
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);//设置右上角×自动结束程序
        frame.setLocationRelativeTo(null);//将窗口置于屏幕的中央
        frame.setVisible(true);//设置窗口可见
    }

    private void initFrame() {

        //顶部
        JPanel titlePanel = new JPanel();//创建标题面板
        titlePanel.setLayout(new FlowLayout());//设置标题面板为流式布局管理器
        titlePanel.add(new JLabel("欢迎访问二嗨租车"));
        container.add(titlePanel,"North");

        //中部表单
        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(null);
        JLabel username_label = new JLabel("用户名:");
        username_label.setBounds(50, 20, 50, 20);
        JLabel password_label = new JLabel("密    码:");
        password_label.setBounds(50, 60, 50, 20);
        fieldPanel.add(username_label);
        fieldPanel.add(password_label);
        username.setBounds(110,20,120,20);
        password.setBounds(110,60,120,20);
        fieldPanel.add(username);
        fieldPanel.add(password);
        container.add(fieldPanel,"Center");

        //底部按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(login);
        buttonPanel.add(register);
        container.add(buttonPanel,"South");

        //点击登录按钮执行
        login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String t_username = username.getText();//获取输入的值
                String t_password = password.getText();
                //TODO
                User user = new Login().login(t_username, t_password);
                if (user != null){
                    hide();
                    new SuccessLoginDialog(this, "登录成功提示", true, user);
                }else{
                    new FailLoginDialog(this, "登录失败提示", true);
                }
            }
        });

        //点击注册按钮执行
        register.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //TODO
                hide();//将登录页面设为不可见
                new RegisterUI();//跳转注册页面
            }
        });
    }

    public void hide() {
        frame.setVisible(false);
    }

    //登录成功提示对话框
    class SuccessLoginDialog extends JDialog{
        private User user;
        public SuccessLoginDialog(ActionListener owner, String title, boolean modal, User user){
            init(user);
            this.setVisible(true);
        }

        private void init(User user){
            JPanel panel = new JPanel();
            Icon success = new ImageIcon("src\\com\\superman\\view\\icon\\success.png");
            JLabel desc = new JLabel("登录成功！欢迎您" + user.getUsername() , success, SwingConstants.CENTER);
            JButton btn = new JButton("确定");

            panel.add(btn);
            this.add(desc, BorderLayout.CENTER);
            this.add(panel, BorderLayout.SOUTH);
            this.setSize(300, 200);

            //设置弹出窗口位置
            int fx = frame.getLocation().x;
            int fy = frame.getLocation().y;
            int window_width = frame.getWidth();
            int window_height = frame.getHeight();
            int dialog_width = this.getWidth();
            int dialog_height = this.getHeight();
            this.setLocation(fx + (window_width - dialog_width)/2, fy + (window_height - dialog_height)/2);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    hide();
                    //TODO 登录成功跳转  0 跳转用户起始页  1 跳转管理员起始页
                    switch (user.getType()){
                        case 0:
                            new UserUI(user);
                            break;
                        case 1:
                            new AdminUI(user);
                            break;
                    }

                }
            });
        }
    }

    //登录失败提示对话框
    class FailLoginDialog extends JDialog{
        public FailLoginDialog(ActionListener owner, String title, boolean modal){
            init();
            this.setVisible(true);
        }

        private void init(){
            JPanel panel = new JPanel();
            Icon fail = new ImageIcon("src\\com\\superman\\view\\icon\\fail.png");
            JLabel desc = new JLabel("用户名或密码错误，登录失败", fail, SwingConstants.CENTER);
            JButton register_button = new JButton("重新登录");

            panel.add(register_button);
            this.add(desc, BorderLayout.CENTER);
            this.add(panel, BorderLayout.SOUTH);
            this.setSize(300, 200);

            //设置弹出窗口位置
            int fx = frame.getLocation().x;
            int fy = frame.getLocation().y;
            int window_width = frame.getWidth();
            int window_height = frame.getHeight();
            int dialog_width = this.getWidth();
            int dialog_height = this.getHeight();
            this.setLocation(fx + (window_width - dialog_width)/2, fy + (window_height - dialog_height)/2);

            register_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    hide();
                    //TODO 登录失败重新登录
                    LoginUI.this.frame.setVisible(true);
                }
            });
        }
    }
}

